#ifndef __CORE_OPENCL_TENSOR_H__
#define __CORE_OPENCL_TENSOR_H__

#ifdef USE_OPENCL

#include "CL/cl.h"
#include "core/device.h"

namespace llm_core
{

class OpenCLTensor: public Tensor
{
public:
    OpenCLTensor() {}

    OpenCLTensor(const Shape& shape, const Device& device=Device(),
        const Layout& layout=LAYOUT_NCHW, const DataType& data_type=DATA_TYPE_FP16)
    {
    }
    ~OpenCLTensor() {}

    struct CLParams: public Params
    {
        cl_context context;
        cl_mem_flags flags;
        void* host_ptr; // 为NULL时，表示不需要初始化
        cl_mem data;
    };

    Status FastAlloc()
    {
        if(m_param.data) {
            return LLM_REALLOC_BUFFER;
        }
        OPENCLMMParams& params = m_param;
        cl_int err = 0;
        params.data = clCreateBuffer(params.context, params.flags,
            m_param.alloc_bytes, params.host_ptr, &err);
        if(err != CL_SUCCESS) {
            return LLM_CL_CREATE_BUFFER_FAILED;
        }
        return LLM_SUCCESS;
    }

    Status FastFree() {
        if (!m_param.data) {
            return LLM_INVALID_PARAM;
        }

        cl_int err = 0;
        clReleaseMemObject(m_param.data);
        m_data = nullptr;
        return LLM_SUCCESS;
    }

private:
    CLParams m_param;
}; // class CPUTensor


} // namespace llm_core

#endif // USE_OPENCL

#endif // __CORE_OPENCL_TENSOR_H__